/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Facility;
import healthcare.coursework.model.FacilityStatus;
import healthcare.coursework.service.FacilityService;
import java.util.List;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zkplus.databind.BindingListModelList;

import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;

/**
 *
 * @author Administrator
 */
public class TrackResources extends GenericAutowireComposer {

    //Componennt
    private Button facilityQuery;
    private Listbox resourceDataListView;
    private Combobox resourceName;
    private Button status;
    //Service
    private FacilityService facilityService = util.Utility.getUtility().getFacilityService();
    private List<Facility> facilities;

    public void onLoadForm(Event e) {
        setInfo();
    }

    private void setInfo() {
        doFilter();
        resourceName.setModel(new ListModelList(facilities, true));
    }

    public void onResourceQuery(Event event) {
        doFilter();
    }

    public void onStatus(Event event) {
        Facility f = (Facility) resourceDataListView.getSelectedItem().getValue();
        if (f.getStatus().toString().equals("bookable")) {
            status.setLabel("Change to " + FacilityStatus.booked);
        } else {
            status.setLabel("Change to " + FacilityStatus.bookable);
        }
    }

    public void onDispatch(Event e) {
        System.out.println("Dispatch");
        Facility f = (Facility) resourceDataListView.getSelectedItem().getValue();
        if (f.getStatus().toString().equals("bookable")) {
            f.setStatus(FacilityStatus.booked);
        } else {
            f.setStatus(FacilityStatus.bookable);
        }
        facilityService.updateFacility(f);
        setInfo();
    }

    private void doFilter() {
        try {
            if (resourceName.getText() == null || resourceName.getText().trim().equals("")) {
                facilities = facilityService.listOfFacilities();
            } else {
                facilities = facilityService.listOfFacilities(resourceName.getValue());


            }
        } catch (Exception e) {
            facilities = facilityService.listOfFacilities();
        }

        resourceDataListView.setModel(new ListModelList(facilities, true));

    }
}
